/*
 * 文件名：AddressPropertiesManager.java
 * 版权：(C)版权所有2013-2013 com.zilla
 * 描述：地址配置参数管理类
 * 修改人：ze.chen
 * 修改时间：2013-6-7 下午4:04:34
 */
package com.zilla.android.zillacore.libzilla.file;

import android.content.Context;
import com.zilla.android.zillacore.libzilla.Zilla;
import com.zilla.android.zillacore.libzilla.util.Util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 地址配置参数管理类<br>
 * 直接调用get方法，获取raw/address.properties中对应的value
 *
 * @author ze.chen
 * @version 1.0
 * @date 2013-6-7 下午4:04:34
 */
public class AddressManager {

    private static Properties properties;

    private static AddressManager instance = null;

    // host
    private static String host = "";

    private AddressManager(Context context) {
        InputStream is = null;
        try {
            is = Zilla.APP.getAssets().open("config/address.properties");
            properties = new Properties();
            properties.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            Util.closeStream(is);
        }
    }

    /**
     * 实例化<br>
     * 该方法在系统启动的时候调用
     *
     * @param context
     * @return
     */
    public static AddressManager getInstance(Context context) {
        if (instance == null) {
            instance = new AddressManager(context);
        }
        return instance;
    }

    /**
     * 取值<br>
     * 输入的参数为raw/address.properties中的key
     *
     * @param key
     * @param defaultValue
     * @return
     */
    public static String get(String key, String defaultValue) {
        return properties.getProperty(key, defaultValue);
    }

    /**
     * 取值<br>
     * 输入的参数为raw/address.properties中的key
     *
     * @param key
     * @return
     */
    public static String get(String key) {
        return properties.getProperty(key, "");
    }

    /**
     * 获取完整的URL地址<br>
     * 输入的参数为raw/address.properties中的key
     *
     * @param key
     * @param defaultValue
     * @return
     */
    public static String getUrl(String key, String defaultValue) {
        return getHost() + get(key, defaultValue);
    }

    /**
     * 获取完整的URL地址<br>
     * 输入的参数为raw/address.properties中的key
     *
     * @param key
     * @return
     */
    public static String getUrl(String key) {
        return getHost() + properties.getProperty(key, "");
    }

    /**
     * 获取主机<br>
     * 获取主机地址
     *
     * @return
     */
    public static String getHost() {
//        if (TextUtils.isEmpty(host)) {
            host = properties.getProperty("host", "");
//        }
        return host;
    }

    /**
     * 获取登录主机地址
     * @return
     */
    public static String getLoginHost()
    {
//        if(TextUtils.isEmpty(host))
//        {
            host = properties.getProperty("loginHost","");
//        }
        return host;
    }

}
